home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-9.10-netbook-remix-PL.iso / casper / filesystem.squashfs / usr / share / pyshared / configglue / attributed.py < prev    next >
Text File  |  2009-06-15  |  2KB  |  67 lines

  1. # This file is part of configglue, by John R. Lenton <john.lenton@canonical.com>
  2. # (C) 2009 by Canonical Ltd.
  3. # Released under the BSD License (see the file LICENSE)
  4. # For bug reports, support, and new releases: http://launchpad.net/configglue
  5.  
  6. """
  7. AttributtedConfigParser lives here.
  8. """
  9. import re
  10. from ConfigParser import RawConfigParser
  11.  
  12. marker = object()
  13. class ValueWithAttrs(object):
  14.     """The values returned by AttributtedConfigParser are instances of this.
  15.     """
  16.     def __init__(self, value=marker, **kw):
  17.         self.value = value
  18.         self.attrs = kw
  19.  
  20.     @property
  21.     def is_empty(self):
  22.         """
  23.         Is the value unset?
  24.  
  25.         Note this is different from being set to None.
  26.         """
  27.         return self.value is marker
  28.  
  29. class AttributedConfigParser(RawConfigParser, object):
  30.     """Handle attributed ini-style configuration files
  31.     """
  32.     def optionxform(self, optionstr):
  33.         """See RawConfigParser.optionxform"""
  34.         return optionstr.lower().replace('-', '_')
  35.  
  36.     def normalized_options(self, section):
  37.         """ Return the section's options, removing the attributes.
  38.  
  39.         @param section: The section whose filtered options you want
  40.         @return: A C{set} of option names, with attributes removed
  41.         """
  42.         return set(re.sub(r'\..*', '', option)
  43.                    for option in self.options(section))
  44.  
  45.     def parse_all(self):
  46.         """ Go through all sections and options attempting to parse each one.
  47.         """
  48.         for section in self.sections():
  49.             for option in self.normalized_options(section):
  50.                 self.parse(section, option)
  51.  
  52.     def parse(self, section, option):
  53.         """Parse a single option in a single section.
  54.  
  55.         @param section: the section within which to look for the option
  56.         @param option: the 'base' option to parse
  57.         """
  58.         if self.has_option(section, option):
  59.             value = ValueWithAttrs(self.get(section, option))
  60.         else:
  61.             value = ValueWithAttrs()
  62.         self.set(section, option, value)
  63.         for opt, val in self.items(section)[:]:
  64.             if opt.startswith(option + '.'):
  65.                 value.attrs[opt[len(option)+1:]] = val
  66.                 self.remove_option(section, opt)
  67.